//
//  MesageListViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/3/31.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MesageListViewController.h"
#import "MessageListModel.h"
#import "MesageListCell.h"
#import "MDDOrderDetailViewController.h"
#import "XFWkwebView.h"

@interface MesageListViewController ()
@property (weak, nonatomic) IBOutlet UITableView *selfTableView;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,assign) NSInteger page;
@end

@implementation MesageListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNavigationItems];
    mNavitationTitle(@"我的消息");
    self.page = 1;
    self.dataArr = [[NSMutableArray alloc]init];
    [self loadMsgList];
    self.view.backgroundColor = [UIColor AxcUI_colorWithHexCode:@"#ebebeb"];
}
#pragma mark -
#pragma mark - **************** 数据请求
-(void)loadMsgList
{
    if ([self.selfTableView.mj_header isRefreshing]) {
        self.page = 1;
    }
    if ([self.selfTableView.mj_footer isRefreshing]) {
        self.page ++;
    }
    
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    setDickeyobj(parameters, @"10", @"limit");
    NSString * pageStr = [NSString stringWithFormat:@"%ld",self.page];
    setDickeyobj(parameters, pageStr, @"page");
    setDickeyobj(parameters, @"", @"key");
    [PPNetworkHelper POST:app_message_list parameters:parameters success:^(id responseObject) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
        if (self.page == 1 && self.dataArr.count > 0) {
            [self.dataArr removeAllObjects];
        }
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * dataD in responseObject[@"data"][@"list"]) {
                MessageListModel * item = [[MessageListModel alloc]init];
                [item setValuesForKeysWithDictionary:dataD];
                item.ID = dataD[@"id"];
                [self.dataArr addObject:item];
            }
            
            if ([responseObject[@"data"][@"list"] count] < 10) {
                [self.selfTableView.mj_footer endRefreshingWithNoMoreData];
                
            }
            
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
    } showLoading:nil];
}
#pragma mark -
#pragma mark - **************** tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MesageListCell * itemCell = mGetTableViewCell(@"mesageListCell");
    MessageListModel * itemModel = self.dataArr[indexPath.section];
    itemCell.titleLabel.text = itemModel.title;
    itemCell.timeLabel.text = itemModel.createTime;
    itemCell.contentLabel.text = itemModel.content;
    if ([itemModel.state isEqualToString:@"0"]) {
        itemCell.redImage.hidden =NO;
    } else {
        itemCell.redImage.hidden =YES;
    }
//   *type;//0:系统消息；1活动消息；2订单消息；3无需跳页面消息；

    if ([itemModel.type isEqualToString:@"0"]) {
        
        itemCell.logoImage.image = [UIImage imageNamed:@"我的消息_信息"];
    
    } else if([itemModel.type isEqualToString:@"1"]){
     
        itemCell.logoImage.image = [UIImage imageNamed:@"我的消息_通知"];
    
    }else if([itemModel.type isEqualToString:@"2"]){
        itemCell.logoImage.image = [UIImage imageNamed:@"我的消息_订单"];
    
    }else {
        itemCell.logoImage.image = [UIImage imageNamed:@"我的消息_信息"];
    }
    
    return itemCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 145;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    MessageListModel * itemModel = self.dataArr[indexPath.section];

    if ([itemModel.state isEqualToString:@"0"]) {
        
        NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
        setDickeyobj(parameters, itemModel.ID, @"messageId");
        setDickeyobj(parameters, @"1", @"add");

        [PPNetworkHelper POST:app_message_reading parameters:parameters success:^(id responseObject) {
            
        } failure:^(NSError *error) {
            
        } showLoading:nil];
    }
    
    if ([itemModel.type isEqualToString:@"0"]) {
        
        XFWkwebView * web = [[XFWkwebView alloc]init];
        [web loadInfoHTMLSringForMyMeassage:itemModel.taskId];
        [self.navigationController pushViewController:web animated:YES];
        
    } else if([itemModel.type isEqualToString:@"1"]){
        
        XFWkwebView * web = [[XFWkwebView alloc]init];
        [web loadInfoHTMLSringForMyMeassage:itemModel.taskId];
        [self.navigationController pushViewController:web animated:YES];
        
        
    }else if([itemModel.type isEqualToString:@"2"]){
        
        MDDOrderDetailViewController * odvc = mGetStoryboardViewController(@"Main", @"mDDOrderDetailViewController");
        odvc.orderNo = itemModel.taskId;
        NavigationController_pushVC(odvc);
    }else {
        
        
    }
    
}
@end
